package com.example.fangyimanagement;

import com.example.fangyimanagement.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/roleManage")
public class RoleManageController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("/addRole")
    public Result<Void> addRole(@RequestBody Map<String, String> param) {
        String roleCode = param.get("roleCode");
        String roleName = param.get("roleName");
        String remark = param.get("remark");
        // 先查询库里有没有这个角色编码，只有编码要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_role WHERE role_code=?", roleCode);
        if (!count.isEmpty()) {
            return Result.failed("和其他角色的编码重复，请修改~");
        }
        //        操作数据库
        String sql = "insert into glpt_role (role_code, role_name, create_time, remark) " +
                "values (?, ?, CURRENT_TIME, ?)";
        int rows = this.jdbcTemplate.update(sql, roleCode, roleName, remark);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/deleteRole")
    public Result<Void> deleteRole(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        List<Map<String, Object>> roleList = this.jdbcTemplate.queryForList("select * from glpt_role where id=?", id);
        if (!roleList.isEmpty() && roleList.get(0).get("role_code") != null) {
            List<Map<String, Object>> useThisRoleUserList = this.jdbcTemplate.queryForList(
                    "select * from glpt_user where role_code=?", roleList.get(0).get("role_code"));
            if (!useThisRoleUserList.isEmpty()) {
                return Result.failed("有用户正在使用此角色，请先为该用户授予其他角色");
            } else {
                //        操作数据库
                int rows = this.jdbcTemplate.update("delete from glpt_role where id =?", id);
                if (rows > 0) {
                    return Result.success();
                } else {
                    return Result.failed("删除角色失败");
                }
            }
        } else {
            return Result.failed("没找到这个角色");
        }
    }
    @RequestMapping("/updateRole")
    public Result<Void> updateRole(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String roleCode = param.get("roleCode");
        String roleName = param.get("roleName");
        String remark = param.get("remark");
        // 先查询库里有没有这个角色编码，只有编码要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_role WHERE role_code=? AND id !=?", roleCode, id);
        if (!count.isEmpty()) {
            return Result.failed("和其他角色的编码重复，请修改~");
        }
        //        操作数据库
        String sql = "update glpt_role set role_code=?, role_name=?, remark=? where id =?";
        int rows = this.jdbcTemplate.update(sql, roleCode, roleName, remark, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/roleList")
    public Result<List<Map<String, Object>>> roleList(@RequestBody Map<String, String> param) {
        int page = Integer.parseInt(param.get("page"));
        int size = Integer.parseInt(param.get("size"));
        String roleName = param.get("roleName");
        List<Object> paramList = new ArrayList<>();
        String sql = "SELECT id, role_code AS roleCode, role_name AS roleName, create_time AS createTime, remark FROM glpt_role";
        if (roleName != null && !roleName.isEmpty()) {
            sql = sql + " WHERE role_name LIKE '%" + roleName + "%'";
        }

        String countSql = "SELECT COUNT(1) FROM ("+ sql +") t";
        Integer count = this.jdbcTemplate.queryForObject(countSql, Integer.class, paramList.toArray());

        sql = sql + " ORDER BY id LIMIT ?, ?";
        paramList.add((page - 1) * size);
        paramList.add(size);
        List<Map<String, Object>> roleList = this.jdbcTemplate.queryForList(sql, paramList.toArray());

        return Result.success(roleList).setTotal(count);
    }
    @RequestMapping("/allRoleList")
    public Result<List<Map<String, Object>>> allRoleList(@RequestBody Map<String, String> param) {
        List<Map<String, Object>> roleList = this.jdbcTemplate.queryForList("SELECT role_code AS code, role_name AS name FROM glpt_role");
        return Result.success(roleList);
    }
}
